﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class HoaDonBanSachDAO
    {
        public int Insert(
           System.Data.SqlClient.SqlConnection connection,
           System.Data.SqlClient.SqlTransaction transaction,
           DTO.HoaDonBanSachDTO dto)
        {
            int r = -1;
            try
            {
                List<System.Data.SqlClient.SqlParameter> prs =
                   new List<System.Data.SqlClient.SqlParameter>();
                prs.Add(new System.Data.SqlClient.SqlParameter("@IDKhachHang", dto.IDKhachHang));
                prs.Add(new System.Data.SqlClient.SqlParameter("@NgayLap", dto.NgayLap));
                if (connection == null || transaction == null)
                    r = DAL.sqlHelper.ExecuteNoneQuery("HoaDonBanSach_Insert", prs);
                else
                    r = DAL.sqlHelper.ExecuteNoneQuery(connection, transaction, "HoaDonBanSach_Insert", prs);
                return r;
            }
            catch (System.Exception ex)
            {
                throw new System.Exception("Insert failed " + ex.Message);
            }
            finally
            {
            }
        }

        public List<DTO.HoaDonBanSachDTO> SelectByNgayNhap(
            System.Data.SqlClient.SqlConnection connection,
            System.Data.SqlClient.SqlTransaction transaction,
            DateTime NgayLap)
        {
            List<DTO.HoaDonBanSachDTO> r = new List<DTO.HoaDonBanSachDTO>();
            try
            {
                List<System.Data.SqlClient.SqlParameter> prs =
                    new List<System.Data.SqlClient.SqlParameter>();
                prs.Add(new System.Data.SqlClient.SqlParameter("@NgayLap", NgayLap));
                System.Data.DataTable dt = null;
                if (connection == null || transaction == null)
                    dt = DAL.sqlHelper.ExecuteQuery("HoaDonBanSach_SelectAllByNgayLap", prs);
                else
                    dt = DAL.sqlHelper.ExecuteQuery(connection, transaction,
                        "HoaDonBanSach_SelectAllByNgayLap", prs);
                foreach (System.Data.DataRow dr in dt.Rows)
                {
                    DTO.HoaDonBanSachDTO dto = new DTO.HoaDonBanSachDTO();
                    dto.NgayLap = DateTime.Parse(dr["NgayLap"].ToString());
                    dto.ID = int.Parse(dr["ID"].ToString());
                    dto.IDKhachHang = int.Parse(dr["IDKhachHang"].ToString());
                    r.Add(dto);
                }
                return r;
            }
            catch (System.Exception ex)
            {
                throw new System.Exception("SelectByNgayNhap failed " + ex.Message);
            }
            finally
            {
            }
        }
    }
}
